home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cevntlog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  14.4 KB  |  611 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. IMPLEMENT_DYNAMIC( CEventLog, CObject );
  18.  
  19. CEventLog::CEventLog()
  20. {
  21.    m_Initialize();
  22. }
  23.  
  24. CEventLog::CEventLog( LPCTSTR source_name )
  25. {
  26.    m_Initialize();
  27.    RegisterSource( source_name );
  28. }
  29.  
  30. CEventLog::~CEventLog()
  31. {
  32.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  33.    {
  34.       DeregisterSource();
  35.    }
  36.  
  37.    if ( m_LogHandle != INVALID_HANDLE_VALUE )
  38.    {
  39.       Close();
  40.    }
  41.  
  42.    m_Initialize();
  43. }
  44.  
  45. void CEventLog::m_Initialize( void )
  46. {
  47.    ASSERT_VALID( this );
  48.  
  49.    ComputerName.Empty();
  50.    LogName.Empty();
  51.  
  52.    m_LogHandle                 = INVALID_HANDLE_VALUE;
  53.    m_EventSourceHandle         = INVALID_HANDLE_VALUE;
  54.    m_ErrorCode                 = 0;
  55.    m_NumberOfBytesRead         = 0;
  56.    m_NumberOfBytesInNextRecord = 0;
  57. }
  58.  
  59. BOOL CEventLog::Backup( LPCTSTR name_of_backup_file )
  60. {
  61.    ASSERT_VALID( this );
  62.    ASSERT( name_of_backup_file != NULL );
  63.  
  64.    if ( name_of_backup_file == NULL )
  65.    {
  66.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  67.       return( FALSE );
  68.    }
  69.  
  70.    BOOL return_value = ::BackupEventLog( m_LogHandle, name_of_backup_file );
  71.  
  72.    if ( return_value != TRUE )
  73.    {
  74.       m_ErrorCode = ::GetLastError();
  75.    }
  76.  
  77.    return( return_value );
  78. }
  79.  
  80. BOOL CEventLog::Clear( LPCTSTR name_of_backup_file )
  81. {
  82.    ASSERT_VALID( this );
  83.  
  84.    /*
  85.    ** name_of_backup_file can be NULL
  86.    */
  87.  
  88.    BOOL return_value = ::ClearEventLog( m_LogHandle, name_of_backup_file );
  89.  
  90.    if ( return_value != TRUE )
  91.    {
  92.       m_ErrorCode = ::GetLastError();
  93.    }
  94.  
  95.    return( return_value );
  96. }
  97.  
  98. BOOL CEventLog::Close( void )
  99. {
  100.    ASSERT_VALID( this );
  101.  
  102.    BOOL return_value = ::CloseEventLog( m_LogHandle );
  103.  
  104.    if ( return_value != TRUE )
  105.    {
  106.       m_ErrorCode = ::GetLastError();
  107.    }
  108.  
  109.    m_LogHandle = INVALID_HANDLE_VALUE;
  110.  
  111.    return( return_value );
  112. }
  113.  
  114. BOOL CEventLog::CreateApplicationLog( LPCTSTR application_name, LPCTSTR message_resource_file, DWORD supported_types )
  115. {
  116.    ASSERT_VALID( this );
  117.    ASSERT( application_name      != NULL );
  118.    ASSERT( message_resource_file != NULL );
  119.  
  120.    if ( application_name == NULL || message_resource_file == NULL )
  121.    {
  122.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  123.       return( FALSE );
  124.    }
  125.  
  126.    if ( application_name[ 0 ] == 0x00 || message_resource_file[ 0 ] == 0x00 )
  127.    {
  128.       return( FALSE );
  129.    }
  130.  
  131.    CRegistry registry;
  132.  
  133.    if ( registry.Connect( CRegistry::keyLocalMachine ) != TRUE )
  134.    {
  135.       m_ErrorCode = registry.GetErrorCode();
  136.       return( FALSE );
  137.    }
  138.  
  139.    CString log_key_name( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\" );
  140.  
  141.    log_key_name += application_name;
  142.  
  143.    if ( registry.Create( log_key_name ) != TRUE )
  144.    {
  145.       m_ErrorCode = registry.GetErrorCode();
  146.       return( FALSE );
  147.    }
  148.  
  149.    if ( registry.SetValue( "EventMessageFile", CRegistry::typeUnexpandedString, (LPBYTE) message_resource_file, strlen( message_resource_file ) + 1 ) != TRUE )
  150.    {
  151.       m_ErrorCode = registry.GetErrorCode();
  152.       return( FALSE );
  153.    }
  154.  
  155.    if ( registry.SetValue( "TypesSupported", supported_types ) != TRUE )
  156.    {
  157.       m_ErrorCode = registry.GetErrorCode();
  158.       return( FALSE );
  159.    }
  160.  
  161.    return( TRUE );
  162. }
  163.  
  164. BOOL CEventLog::DeleteApplicationLog( LPCTSTR application_name )
  165. {
  166.    ASSERT_VALID( this );
  167.    ASSERT( application_name != NULL );
  168.  
  169.    if ( application_name == NULL )
  170.    {
  171.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  172.       return( FALSE );
  173.    }
  174.  
  175.    if ( application_name[ 0 ] == 0x00 )
  176.    {
  177.       return( FALSE );
  178.    }
  179.  
  180.    CRegistry registry;
  181.  
  182.    if ( registry.Connect( CRegistry::keyLocalMachine ) != TRUE )
  183.    {
  184.       m_ErrorCode = registry.GetErrorCode();
  185.       return( FALSE );
  186.    }
  187.  
  188.    CString log_key_name( "SYSTEM\\CurrentControl\\Services\\EventLog\\Application\\" );
  189.  
  190.    log_key_name += application_name;
  191.  
  192.    if ( registry.DeleteKey( log_key_name ) != TRUE )
  193.    {
  194.       m_ErrorCode = registry.GetErrorCode();
  195.       return( FALSE );
  196.    }
  197.  
  198.    /*
  199.    ** Microsoft has a bug in this area. Even though we deleted the application from the
  200.    ** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\
  201.    ** registry area, they don't provide a way to delete the application from the 
  202.    ** HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\Sources
  203.    ** value. The application name is one of the strings in this REG_MULTI_SZ value. We
  204.    ** still need to delete it from there. The names listed in this value appear in the 
  205.    ** "Source" combobox of the Event Viewer application View->Filter Events... menu selection.
  206.    */
  207.  
  208.    if ( registry.Open( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application" ) == TRUE )
  209.    {
  210.       CStringArray sources;
  211.  
  212.       if ( registry.GetValue( "Sources", sources ) == TRUE )
  213.       {
  214.          int index = 0;
  215.          int number_of_sources = sources.GetSize();
  216.  
  217.          BOOL application_was_found = FALSE;
  218.  
  219.          while( index < number_of_sources )
  220.          {
  221.             if ( sources[ index ] == application_name )
  222.             {
  223.                application_was_found = TRUE;
  224.                sources.RemoveAt( index );
  225.                index = number_of_sources;
  226.             }
  227.  
  228.             index++;
  229.          }
  230.  
  231.          if ( application_was_found == TRUE )
  232.          {
  233.             registry.SetValue( "Sources", sources );
  234.          }
  235.       }
  236.    }
  237.  
  238.    return( TRUE );
  239. }
  240.  
  241. BOOL CEventLog::DeregisterSource( void )
  242. {
  243.    ASSERT_VALID( this );
  244.  
  245.    BOOL return_value = TRUE;
  246.  
  247.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  248.    {
  249.       return_value = ::DeregisterEventSource( m_EventSourceHandle );
  250.  
  251.       if ( return_value != TRUE )
  252.       {
  253.          m_ErrorCode = ::GetLastError();
  254.       }
  255.  
  256.       m_EventSourceHandle = INVALID_HANDLE_VALUE;
  257.    }
  258.  
  259.    return( return_value );
  260. }
  261.  
  262. DWORD CEventLog::GetErrorCode( void ) const
  263. {
  264.    ASSERT_VALID( this );
  265.    return( m_ErrorCode );
  266. }
  267.  
  268. BOOL CEventLog::GetNumberOfRecords( DWORD& number_of_records )
  269. {
  270.    ASSERT_VALID( this );
  271.  
  272.    BOOL return_value = ::GetNumberOfEventLogRecords( m_LogHandle, &number_of_records );
  273.  
  274.    if ( return_value != TRUE )
  275.    {
  276.       m_ErrorCode = ::GetLastError();
  277.    }
  278.  
  279.    return( return_value );
  280. }
  281.  
  282. BOOL CEventLog::NotifyChange( HANDLE event_handle, HANDLE user_log_handle )
  283. {
  284.    ASSERT_VALID( this );
  285.    ASSERT( event_handle != INVALID_HANDLE_VALUE );
  286.  
  287.    if ( event_handle == INVALID_HANDLE_VALUE )
  288.    {
  289.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  290.       return( FALSE );
  291.    }
  292.  
  293.    if ( user_log_handle == INVALID_HANDLE_VALUE || user_log_handle == NULL )
  294.    {
  295.       user_log_handle = m_LogHandle;
  296.    }
  297.  
  298.    BOOL return_value = ::NotifyChangeEventLog( user_log_handle, event_handle );
  299.  
  300.    if ( return_value != TRUE )
  301.    {
  302.       m_ErrorCode = ::GetLastError();
  303.    }
  304.  
  305.    return( return_value );
  306. }
  307.  
  308. BOOL CEventLog::Open( LPCTSTR log_name, LPCTSTR name_of_computer )
  309. {
  310.    ASSERT_VALID( this );
  311.    ASSERT( log_name != NULL );
  312.  
  313.    /*
  314.    ** name_of_computer can be NULL
  315.    */
  316.                  
  317.    if ( log_name == NULL )
  318.    {
  319.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  320.       return( FALSE );
  321.    }
  322.  
  323.    BOOL return_value = TRUE;
  324.  
  325.    m_LogHandle = ::OpenEventLog( name_of_computer, log_name );
  326.  
  327.    if ( m_LogHandle == NULL )
  328.    {
  329.       m_LogHandle  = INVALID_HANDLE_VALUE;
  330.       m_ErrorCode  = ::GetLastError();
  331.       return_value = FALSE;
  332.    }
  333.    else
  334.    {
  335.       if ( name_of_computer == NULL )
  336.       {
  337.          TCHAR computer_name[ MAX_PATH ] = "";
  338.          DWORD size = sizeof( computer_name );
  339.  
  340.          if ( ::GetComputerName( computer_name, &size ) == TRUE )
  341.          {
  342.             ComputerName = computer_name;
  343.          }
  344.          else
  345.          {
  346.             ComputerName.Empty();
  347.          }
  348.       }
  349.       else
  350.       {
  351.          ComputerName = name_of_computer;
  352.       }
  353.    }
  354.  
  355.    return( return_value );
  356. }
  357.  
  358. BOOL CEventLog::OpenBackup( LPCTSTR name_of_backup_file, LPCTSTR name_of_computer )
  359. {
  360.    ASSERT_VALID( this );
  361.    ASSERT( name_of_backup_file != NULL );
  362.  
  363.    /*
  364.    ** name_of_computer can be NULL
  365.    */
  366.  
  367.    if ( name_of_backup_file == NULL )
  368.    {
  369.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  370.       return( FALSE );
  371.    }
  372.  
  373.    BOOL return_value = TRUE;
  374.  
  375.    m_LogHandle = ::OpenBackupEventLog( name_of_computer, name_of_backup_file );
  376.  
  377.    if ( m_LogHandle == NULL )
  378.    {
  379.       m_LogHandle = INVALID_HANDLE_VALUE;
  380.       m_ErrorCode = ::GetLastError();
  381.       return_value = FALSE;
  382.    }
  383.    else
  384.    {
  385.       if ( name_of_computer == NULL )
  386.       {
  387.          TCHAR computer_name[ MAX_PATH ] = "";
  388.          DWORD size = sizeof( computer_name );
  389.  
  390.          if ( ::GetComputerName( computer_name, &size ) == TRUE )
  391.          {
  392.             ComputerName = computer_name;
  393.          }
  394.          else
  395.          {
  396.             ComputerName.Empty();
  397.          }
  398.       }
  399.       else
  400.       {
  401.          ComputerName = name_of_computer;
  402.       }
  403.    }
  404.  
  405.    return( return_value );
  406. }
  407.  
  408. BOOL CEventLog::Read( DWORD record_number, LPVOID buffer, DWORD& number_of_bytes_to_read, DWORD how_to_read )
  409. {
  410.    ASSERT_VALID( this );
  411.    ASSERT( buffer != NULL );
  412.  
  413.    if ( buffer == NULL )
  414.    {
  415.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  416.       return( FALSE );
  417.    }
  418.  
  419.    BOOL return_value = ::ReadEventLog( m_LogHandle,
  420.                                        how_to_read,
  421.                                        record_number,
  422.                                        buffer,
  423.                                        number_of_bytes_to_read,
  424.                                       &m_NumberOfBytesRead,
  425.                                       &m_NumberOfBytesInNextRecord );
  426.  
  427.    if ( return_value != TRUE )
  428.    {
  429.       m_ErrorCode = ::GetLastError();
  430.    }
  431.  
  432.    return( return_value );
  433. }
  434.  
  435. BOOL CEventLog::RegisterSource( LPCTSTR source_name, LPCTSTR name_of_computer )
  436. {
  437.    ASSERT_VALID( this );
  438.    ASSERT( source_name != NULL );
  439.  
  440.    /*
  441.    ** name_of_computer can be NULL
  442.    */
  443.  
  444.    if ( source_name == NULL )
  445.    {
  446.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  447.       return( FALSE );
  448.    }
  449.  
  450.    BOOL return_value = TRUE;
  451.  
  452.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  453.    {
  454.       DeregisterSource();
  455.    }
  456.  
  457.    m_EventSourceHandle = ::RegisterEventSource( name_of_computer, source_name );
  458.  
  459.    if ( m_EventSourceHandle == NULL )
  460.    {
  461.       TRACE( "RegisterEventSource returned NULL\n" );
  462.       m_EventSourceHandle = INVALID_HANDLE_VALUE;
  463.       m_ErrorCode          = ::GetLastError();
  464.       return_value        = FALSE;
  465.    }
  466.  
  467.    return( return_value );
  468. }
  469.  
  470. BOOL CEventLog::Report( EventType event_type,
  471.                         WORD      category,
  472.                         DWORD     event_id,
  473.                         WORD      number_of_strings,
  474.                         LPCTSTR * string_array,
  475.                         DWORD     number_of_raw_data_bytes,
  476.                         LPVOID    raw_data_buffer,
  477.                         PSID      user_sid )
  478. {
  479.    ASSERT_VALID( this );
  480.  
  481.    BYTE temp_sid[ 4096 ];
  482.  
  483.    DWORD sid_size = sizeof( temp_sid );
  484.  
  485.    if ( user_sid == NULL )
  486.    {
  487.       TCHAR user_name[ 256 ];
  488.       DWORD size_of_user_name  = sizeof( user_name );
  489.  
  490.       TCHAR domain_name[ 256 ];
  491.       DWORD size_of_domain_name = sizeof( domain_name );
  492.  
  493.       SID_NAME_USE type_of_sid;
  494.  
  495.       ::ZeroMemory( user_name, size_of_user_name );
  496.       ::ZeroMemory( domain_name, size_of_domain_name );
  497.       ::ZeroMemory( temp_sid, sid_size );
  498.  
  499.       TRACE( "CEventLog::Report(), Calling GetUserName()\n" );
  500.       ::GetUserName( user_name, &size_of_user_name );
  501.  
  502.       TRACE( "CEventLog::Report(), Calling LookupAccountName()\n" );
  503.       if ( ::LookupAccountName( NULL,
  504.                                 user_name,
  505.                                &temp_sid,
  506.                                &sid_size,
  507.                                 domain_name,
  508.                                &size_of_domain_name,
  509.                                &type_of_sid ) == TRUE )
  510.       {
  511.          user_sid = temp_sid;
  512.       }
  513.    }
  514.  
  515.    BOOL return_value = FALSE;
  516.  
  517.    if ( m_EventSourceHandle != INVALID_HANDLE_VALUE )
  518.    {
  519.       return_value = ::ReportEvent( m_EventSourceHandle,
  520.                                     event_type,
  521.                                     category,
  522.                                     event_id,
  523.                                     user_sid,
  524.                                     number_of_strings,
  525.                                     number_of_raw_data_bytes,
  526.                                     string_array,
  527.                                     raw_data_buffer );
  528.  
  529.       TRACE( "CEventLog::Report(), Calling ReportEvent() went OK\n" );
  530.  
  531.       if ( return_value != TRUE )
  532.       {
  533.          m_ErrorCode = ::GetLastError();
  534.       }
  535.    }
  536.    else
  537.    {
  538.       m_ErrorCode = ERROR_INVALID_HANDLE;
  539.    }
  540.  
  541.    return( return_value );
  542. }
  543.  
  544. BOOL CEventLog::Report( LPCTSTR log_name, DWORD message_string_resource_id, WORD number_of_strings, LPCTSTR* string_array )
  545. {
  546.    ASSERT_VALID( this );
  547.    ASSERT( log_name != NULL );
  548.  
  549.    if ( log_name == NULL )
  550.    {
  551.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  552.       return( FALSE );
  553.    }
  554.  
  555.    if ( string_array == (LPCTSTR *) NULL )
  556.    {
  557.       m_ErrorCode = ERROR_INVALID_HANDLE;
  558.       return( FALSE );
  559.    }
  560.  
  561.    if ( RegisterSource( log_name ) == TRUE )
  562.    {
  563.       if ( Report( eventError, 0, message_string_resource_id, number_of_strings, string_array ) != TRUE )
  564.       {
  565.          DeregisterSource();
  566.          return( FALSE );
  567.       }
  568.  
  569.       DeregisterSource();
  570.    }
  571.    else
  572.    {
  573.       return( FALSE );
  574.    }
  575.  
  576.    return( TRUE );
  577. }
  578.  
  579. void CEventLog::ReportError( LPCTSTR string_to_report )
  580. {
  581.    LPCTSTR string_array[ 1 ];
  582.  
  583.    if ( string_to_report == (LPCTSTR) NULL )
  584.    {
  585.       string_array[ 0 ] = "CEventLog::ReportError( NULL )";
  586.    }
  587.    else
  588.    {
  589.       string_array[ 0 ] = string_to_report;
  590.    }
  591.  
  592.    TRACE( "CEventLog::ReportError()\n" );
  593.    Report( eventError, 0, 0, 1, string_array );
  594. }
  595.  
  596. void CEventLog::ReportInformation( LPCTSTR string_to_report )
  597. {
  598.    LPCTSTR string_array[ 1 ];
  599.  
  600.    if ( string_to_report == (LPCTSTR) NULL )
  601.    {
  602.       string_array[ 0 ] = "CEventLog::ReportInformation( NULL )";
  603.    }
  604.    else
  605.    {
  606.       string_array[ 0 ] = string_to_report;
  607.    }
  608.  
  609.    Report( eventInformation, 0, 0, 1, string_array );
  610. }
  611.